package com.grq.mis.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.grq.mis.entity.CertType;
import com.grq.mis.entity.Item;
import com.grq.mis.exception.BusinessException;
import com.grq.mis.exception.ResultEnum;
import com.grq.mis.model.sys.Me;
import com.grq.mis.service.CertTypeService;
import com.grq.mis.service.ItemService;
import com.grq.mis.validate.ItemValidate;
import org.beetl.sql.core.page.PageResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/item")
public class ItemController extends BaseController {

    @Autowired
    private ItemService itemService;
    @Autowired
    private CertTypeService certTypeService;

    /**
     * 商品列表
     *
     * @param model
     * @return
     */
    @SaCheckPermission("item:list")
    @GetMapping("/page")
    public String page(
            Model model,
            @RequestParam(defaultValue = "1") long page,
            @RequestParam(defaultValue = "20") int size,
            String key
    ) {

        Me me = me();

        // 带入参数
        Map<String, Object> paras = new HashMap<>();
        if (!StringUtils.isEmpty(key)) {
            paras.put("key", "%" + key.trim() + "%");
        }
        paras.put("isAdmin",me.getAdmin());
        paras.put("departmentId",me.getDepartmentId());

        PageResult<Item> result = itemService.page("item.page", paras, page, size);
        model.addAttribute("data", result);

        model.addAttribute("key", key);

        return "item/page.html";
    }

    /**
     * 添加商品页面
     *
     * @return
     */
    @SaCheckPermission("item:add")
    @GetMapping("/addForm")
    public String addForm(Model model) {

        List<CertType> types = certTypeService.createLambdaQuery().andEq(CertType::getIsDisable,false).select();
        model.addAttribute("types",types);

        return "item/add.html";
    }

    /**
     * 添加商品
     * @param item
     * @param model
     * @param img
     * @return
     */
    @SaCheckPermission("item:add")
    @PostMapping("/add")
    public String add(Item item, Model model, MultipartFile img) {

        // 验证
        ItemValidate.add(item);

        Me me = me();
        itemService.addItem(item,me,img);

        return frameSuccess(model, ResultEnum.SUCCESS, "添加商品成功");
    }

    @SaCheckPermission("item:update")
    @GetMapping("/updateForm")
    public String updateForm(@RequestParam Long id, Model model) {

        Item item = itemService.single(id);
        if (item == null) {
            throw new BusinessException(ResultEnum.ITEM_IS_NULL);
        }

        model.addAttribute("item", item);

        List<CertType> types = certTypeService.createLambdaQuery().andEq(CertType::getIsDisable,false).select();
        model.addAttribute("types",types);

        return "item/update.html";
    }

    @SaCheckPermission("item:update")
    @PostMapping("/update")
    public String update(Item item, Model model, MultipartFile img) {

        // 验证
        ItemValidate.update(item);

        Me me = me();
        itemService.updateItem(item,img,me);

        return frameSuccess(model, ResultEnum.SUCCESS, "修改商品成功");
    }

    @SaCheckPermission("item:delete")
    @GetMapping("/delete")
    public String delete(@RequestParam Long id, RedirectAttributes attr) {

        Me me = me();
        itemService.deleteItem(id,me);

        return pageSuccess(attr, "/item/page", ResultEnum.SUCCESS, "删除商品成功");
    }

}
